package com.luoqiz.design.singleton.type3;

public class SingletonTest03 {
    public static void main(String[] args) {
        Singleton singleton1 = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();

        System.out.println("singleton1 hashcode:" + singleton1.hashCode());
        System.out.println("singleton2 hashcode:" + singleton2.hashCode());
    }
}

//懒汉式 （线程不安全）  不推荐  原因：多线程情况下有可能第一个线程尚未实例化，第二个线程也进入初始化代码中
class Singleton {

    public static volatile Singleton singleton;

    public static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton();
        }
        return singleton;
    }

}